package com.yunbao.hujin.mychoosealbum.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.yunbao.hujin.mychoosealbum.R;
import com.yunbao.hujin.mychoosealbum.utils.Const;
import com.yunbao.hujin.mychoosealbum.utils.GlideUtil;
import com.yunbao.hujin.mychoosealbum.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 所有照片的适配器 on 2017/11/17.
 */

public class AdapterCameraImage extends RecyclerView.Adapter<AdapterCameraImage.Holder> {

    private Context context;
    private List<String> gridList;
    private List<String> selectList;    //选中的照片的路径集合
    private int counts;                 //最多选择图片的张数

    public AdapterCameraImage(Context context, List<String> gridList, int counts) {
        this.context = context;
        this.gridList = gridList;
        this.selectList = new ArrayList<>();
        this.counts = counts;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_camera_image, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        if (gridList.get(position).equals(Const.PHOTO)) {
            //如果该集合里面有一项为(.PHOTO)
            holder.box.setVisibility(View.GONE);
            GlideUtil.showLocalImg(context, R.mipmap.liaotian_zhaoxiangjipaizhao)
                    .into(holder.img);
        } else {
            holder.box.setVisibility(View.VISIBLE);
            GlideUtil.showCenterCrop(context, gridList.get(position))
                    .centerCrop()
                    .placeholder(R.drawable.bg_loading_white)
                    .error(R.mipmap.load_failed)
                    .into(holder.img);
        }

        if (selectList.contains(gridList.get(position))) {
            holder.box.setImageResource(R.mipmap.liaotian_quanquangou);
            holder.root.setBackgroundResource(R.drawable.shape_rect_orange);

        } else {
            holder.box.setImageResource(R.mipmap.liaotian_quanquan);
            holder.root.setBackground(null);
        }

        holder.click.setOnClickListener(new MyClick(holder.box,holder.root, gridList.get(position)));
    }

    @Override
    public int getItemCount() {
        return gridList.size();
    }


    /**
     * 得到选中的照片的地址
     *
     * @return 返回选中照片在手机中的路径地址
     */
    public List<String> getSelectList() {
        return selectList;
    }

    class Holder extends RecyclerView.ViewHolder {
        RelativeLayout root;
        ImageView img, box;
        View click;

        public Holder(View itemView) {
            super(itemView);
            root = itemView.findViewById(R.id.root);
            img = itemView.findViewById(R.id.grid_img);
            box = itemView.findViewById(R.id.grid_checkBox);
            click = itemView.findViewById(R.id.grid_click);

            //Grid布局的点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onGridItemClick != null) {
                        onGridItemClick.onItemClick(getLayoutPosition());
                    }
                }
            });
        }
    }

    class MyClick implements View.OnClickListener {
        RelativeLayout root;
        ImageView box;
        String path;

        public MyClick(ImageView box,RelativeLayout root, String path) {
            this.box = box;
            this.root = root;
            this.path = path;
        }

        @Override
        public void onClick(View v) {
            if (selectList.size() < counts) {
                if (selectList.contains(path)) {
                    box.setImageResource(R.mipmap.liaotian_quanquan);
                    root.setBackground(null);
                    selectList.remove(path);
                } else {
                    box.setImageResource(R.mipmap.liaotian_quanquangou);
                    root.setBackgroundResource(R.drawable.shape_rect_orange);
                    selectList.add(path);
                }
            } else {
                if (selectList.contains(path)) {
                    box.setImageResource(R.mipmap.liaotian_quanquan);
                    root.setBackground(null);
                    selectList.remove(path);
                } else {
                    ToastUtil.ToastMsg(context, "最多选择" + counts + "张图片");
                }
            }
        }
    }


    //接口用于点击事件
    private OnGridItemClick onGridItemClick;      //下面接口

    public interface OnGridItemClick {
        void onItemClick(int position);
    }

    public void setOnGridItemClick(OnGridItemClick onGridItemClick) {
        this.onGridItemClick = onGridItemClick;
    }

}
